import launch
import launch.launch_description_source
import launch.launch_description_sources
import launch_ros
from ament_index_python.packages import get_package_share_directory

def generate_launch_description():
    '''产生launch描述'''

    action_declare_start_rqt= launch.actions.DeclareLaunchArgument('start_rqt',default_value="False")
    start_rqt=launch.substitutions.LaunchConfiguration('start_rqt',default="False")


    # 动作一，启动其他launch
    launch_cmd=[get_package_share_directory('turtlesim'),'/launch/','multisim.launch.py']
    action_include_launch=launch.actions.IncludeLaunchDescription(
        launch.launch_description_sources.PythonLaunchDescriptionSource(
            launch_cmd
        )
    )

    #动作二，打印数据
    action_log_info=launch.actions.LogInfo(msg=str(action_include_launch))

    #动作三，执行进程，如ros2 topic list
    action_topic_list=launch.actions.ExecuteProcess(
        cmd=['ros2','topic','list'],
        output='screen'
    )

    # 参数判断动作是否执行
    action_rqt=launch.actions.ExecuteProcess(

        condition=launch.conditions.IfCondition(start_rqt) , 
        cmd=['rqt'],
        
    )

    #动作四，组织动作组成，多个动作合一
    action_group=launch.actions.GroupAction([
        launch.actions.TimerAction(period=2.0,actions=[action_include_launch]),
        # launch.actions.TimerAction(period=2.0,actions=[action_log_info]),
        # launch.actions.TimerAction(period=2.0,actions=[action_topic_list]),
        launch.actions.TimerAction(period=2.0,actions=[action_rqt]),
    ])  

    return launch.LaunchDescription([
       action_group,
       action_declare_start_rqt,
    ])